package com.sky.controller.admin;


import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@RestController
@RequestMapping("/admin/report")
@Api(tags = "商家数据统计相关接口")
@Slf4j
public class ReportController {

    @Autowired
    private ReportService reportService;

    @GetMapping("/turnoverStatistics")
    @ApiOperation("营业额统计")
    public Result<TurnoverReportVO> turnoverStatistics(String begin,String end){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        TurnoverReportVO turnoverReportVO = reportService.turnoverStatistics(LocalDate.parse(begin, formatter),LocalDate.parse(end, formatter));
        return Result.success(turnoverReportVO);
    }
    @GetMapping("/userStatistics")
    @ApiOperation("用户统计")
    public Result<UserReportVO> userStatistics(String begin,String end){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        UserReportVO userReportVO = reportService.userStatistics(LocalDate.parse(begin, formatter),LocalDate.parse(end, formatter));
        return Result.success(userReportVO);
    }
    @GetMapping("/ordersStatistics")
    @ApiOperation("订单统计")
    public Result<OrderReportVO> ordersStatistics(String begin,String end){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        OrderReportVO orderReportVO = reportService.ordersStatistics(LocalDate.parse(begin, formatter),LocalDate.parse(end, formatter));
        return Result.success(orderReportVO);
    }
    @GetMapping("/top10")
    @ApiOperation("销量前10菜品或套餐")
    public Result<SalesTop10ReportVO> top10(String begin,String end){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        SalesTop10ReportVO salesTop10ReportVO = reportService.top10(LocalDate.parse(begin, formatter),LocalDate.parse(end, formatter));
        return Result.success(salesTop10ReportVO);
    }
    @GetMapping("/export")
    @ApiOperation("30天数据导出")
    public void export(HttpServletResponse httpServletResponse){
        reportService.export(httpServletResponse);
    }
}
